﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP_RORZE_TOOL.BizLogic;
using ERP_RORZE_TOOL.Common;
using ERP_RORZE_TOOL.Common.ValueObject;
using ERP_RORZE_TOOL.Tool.UserControls;

namespace ERP_RORZE_TOOL.Tool.Forms
{
    public partial class frm_ToolStatusEdit : DevExpress.XtraEditors.XtraForm
    {
        public int ToolStatusId;
        public string ToolStatusName;
        private ucToolStatus parent;

        public frm_ToolStatusEdit(ucToolStatus fToolStatus)
        {
            InitializeComponent();
            btSave.Image = ERP_RORZE_TOOL.Properties.Resources.accept;
            btCancel.Image = ERP_RORZE_TOOL.Properties.Resources.cancel;
            parent = fToolStatus;
            this.CancelButton = this.btCancel;
        }

        private void btSave_Click(object sender, EventArgs e)
        {
            ToolStatusHelper helper = new ToolStatusHelper(new ToolStatus());
            if (GetData(helper.ValueObject))
            {


                if (helper.Update() > 0)
                {

                    if (!Utilities.MessageConfirmationYesNo(ToolConstants.Message.UpdateSuccessAndQuestion))
                    {
                        this.Close();
                    }
                }
                else
                {

                    Utilities.MessageErrorOk(ToolConstants.Message.UpdateFailed);
                }
                parent.BindDatatoGrid();
            }
        }

        private void btCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frm_ToolStatusEdit_Load(object sender, EventArgs e)
        {
            BindData();
        }

        protected override bool ProcessKeyPreview(ref Message m)
        {
            if (m.Msg == 0x0100 && (int)m.WParam == 13)
            {
                this.ProcessTabKey(true);
            }
            return base.ProcessKeyPreview(ref m);
        }
        #region Private methods

        private bool CheckDuplicate(int id)
        {
            ToolStatusHelper helper = new ToolStatusHelper(new ToolStatus());
            helper.ValueObject.StatusID = id;
            DataTable dt = helper.GetOne();
            if (dt.Rows.Count > 0)
                return true;
            return false;

        }
        private bool GetData(ToolStatus o)
        {
            #region Check valid
            if (Utilities.IsNullOrEmpty(txtStatusID.Text.Trim()))
            {
                MessageBox.Show(ToolConstants.Message.ToolStatus.ToolStatusIDCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if (Utilities.IsNullOrEmpty(txtStatusName.Text.Trim()))
            {
                MessageBox.Show(ToolConstants.Message.ToolStatus.ToolStatusNameCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if (string.Compare(txtStatusID.Text.Trim(), ToolStatusId.ToString()) != 0)
            {
                if (CheckDuplicate(int.Parse(txtStatusID.Text.Trim())))
                {
                    MessageBox.Show(ToolConstants.Message.ToolStatus.ToolStatusHasExisted, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return false;
                }
            }

            #endregion

            o.StatusID = int.Parse(txtStatusID.Text);
            o.StatusName = txtStatusName.Text;

            return true;
        }

        //private void BindData(ToolStatus o)
        //{
        //	txtToolStatusID.Text = o.ToolStatusID;
        //	txtToolStatusName.Text = o.ToolStatus_English;
        //	txtToolStatusJapanese.Text = o.ToolStatus_Japanese;
        //}

        private void BindData()
        {
            txtStatusID.Text = ToolStatusId.ToString();
            txtStatusName.Text = ToolStatusName;
        }

        #endregion

    }
}